gdk: Add gdk_cairo_surface_create_from_pixbuf
authorAlexander Larsson <alexl@redhat.com>
Mon, 24 Jun 2013 10:12:43 +0000 (12:12 +0200)
committerAlexander Larsson <alexl@redhat.com>
Wed, 3 Jul 2013 12:34:13 +0000 (14:34 +0200)
gdk/gdkcairo.c
gdk/gdkcairo.h

index 2e1d8dc3f836ff7c981e19cc992fbf915d053b2c..74bc63611aa7a9eadef422ec576a97dc60404ce6 100644 (file)
@@ -176,24 +176,20 @@ gdk_cairo_region (cairo_t         *cr,
 }
 
 /**
- * gdk_cairo_set_source_pixbuf:
- * @cr: a cairo context
+ * gdk_cairo_surface_create_from_pixbuf:
  * @pixbuf: a #GdkPixbuf
- * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
- * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
- *
- * Sets the given pixbuf as the source pattern for @cr.
+ * @scale: the scale of the new surface, or 0 to use same as @window
+ * @for_window: The window this will be drawn to, on %NULL.
  *
- * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
- * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y.
+ * Creates an image surface with the same contents as
+ * the pixbuf.
  *
- * Since: 2.8
+ * Since: 3.10
  */
-void
-gdk_cairo_set_source_pixbuf (cairo_t         *cr,
-                             const GdkPixbuf *pixbuf,
-                             gdouble          pixbuf_x,
-                             gdouble          pixbuf_y)
+cairo_surface_t *
+gdk_cairo_surface_create_from_pixbuf (const GdkPixbuf *pixbuf,
+                                      int scale,
+                                      GdkWindow *for_window)
 {
   gint width = gdk_pixbuf_get_width (pixbuf);
   gint height = gdk_pixbuf_get_height (pixbuf);
@@ -204,7 +200,6 @@ gdk_cairo_set_source_pixbuf (cairo_t         *cr,
   guchar *cairo_pixels;
   cairo_format_t format;
   cairo_surface_t *surface;
-  static const cairo_user_data_key_t key;
   int j;
 
   if (n_channels == 3)
@@ -212,14 +207,13 @@ gdk_cairo_set_source_pixbuf (cairo_t         *cr,
   else
     format = CAIRO_FORMAT_ARGB32;
 
-  cairo_stride = cairo_format_stride_for_width (format, width);
-  cairo_pixels = g_malloc_n (height, cairo_stride);
-  surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
-                                                 format,
-                                                 width, height, cairo_stride);
-
-  cairo_surface_set_user_data (surface, &key,
-                               cairo_pixels, (cairo_destroy_func_t)g_free);
+  surface =
+     gdk_window_create_similar_image_surface (for_window,
+                                             format,
+                                             width, height,
+                                             scale);
+  cairo_stride = cairo_image_surface_get_stride (surface);
+  cairo_pixels = cairo_image_surface_get_data (surface);
 
   for (j = height; j; j--)
     {
@@ -277,6 +271,33 @@ gdk_cairo_set_source_pixbuf (cairo_t         *cr,
       cairo_pixels += cairo_stride;
     }
 
+  cairo_surface_mark_dirty (surface);
+  return surface;
+}
+
+/**
+ * gdk_cairo_set_source_pixbuf:
+ * @cr: a cairo context
+ * @pixbuf: a #GdkPixbuf
+ * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
+ * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
+ *
+ * Sets the given pixbuf as the source pattern for @cr.
+ *
+ * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
+ * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y.
+ *
+ * Since: 2.8
+ */
+void
+gdk_cairo_set_source_pixbuf (cairo_t         *cr,
+                             const GdkPixbuf *pixbuf,
+                             gdouble          pixbuf_x,
+                             gdouble          pixbuf_y)
+{
+  cairo_surface_t *surface;
+
+  surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, 1, NULL);
   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
   cairo_surface_destroy (surface);
 }
index 9cb601dc7175ac72b0971c4e385ab3b0faf5ae9e..a311d8e15c831f2603c88dc7e8f92db4437b47a0 100644 (file)
@@ -66,6 +66,11 @@ GDK_DEPRECATED_IN_3_4_FOR(gdk_cairo_set_source_rgba)
 void       gdk_cairo_set_source_color   (cairo_t              *cr,
                                          const GdkColor       *color);
 
+GDK_AVAILABLE_IN_3_10
+cairo_surface_t * gdk_cairo_surface_create_from_pixbuf      (const GdkPixbuf *pixbuf,
+                                                             int scale,
+                                                             GdkWindow *for_window);
+
 G_END_DECLS
 
 #endif /* __GDK_CAIRO_H__ */